package com.bajiao.admin.controller.store;

import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bajiao.common.api.vo.Result;
import com.bajiao.common.constant._Whole;
import com.bajiao.common.system.vo.LoginUser;
import com.bajiao.common.util.FastJsonHelper;
import com.bajiao.common.util.IDGeneratorUtils;
import com.bajiao.common.util.UUIDGenerator;
import com.bajiao.entity.store.Store;
import com.bajiao.entity.store.StoreApply;
import com.bajiao.service.store.StoreApplyService;
import com.bajiao.service.store.StoreService;
import com.bajiao.vo.store.StoreApplyAdmin;
import com.bajiao.vo.user.UserAdmin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper;
import lombok.extern.slf4j.Slf4j;
import com.bajiao.admin.base.AdminBaseController;
import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 店铺申请表
 * @Author: FanJq
 * @Date: 2024-12-24
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "店铺申请表")
@RestController
@RequestMapping("/store/apply")
public class StoreApplyController extends AdminBaseController<StoreApply, StoreApplyService> {
    @Resource
    private StoreApplyService storeApplyService;
    @Resource
    private StoreService storeService;

    /**
     * @return
     * @description: 分页列表查询
     * @author: FanJq
     * @date: 2024-12-24
     */
    @ApiOperation(value = "店铺申请表-分页列表查询", notes = "店铺申请表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(String title, String storeMobile, String legalPerson, String mobile, Integer status) {
        startPage();
        List<StoreApplyAdmin> resultList = storeApplyService.getAdminList(title, storeMobile, legalPerson, mobile, status);
        return Result.ok(getDataTable(resultList));
    }

    /**
     * @param storeApply
     * @return
     * @description: 添加
     * @author: FanJq
     * @date: 2024-12-24
     */
    @ApiOperation(value = "店铺申请表-添加", notes = "店铺申请表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody StoreApply storeApply) {
        storeApplyService.save(storeApply);
        return Result.ok("添加成功！");
    }

    /**
     * @param storeApply
     * @return
     * @description: 编辑
     * @author: FanJq
     * @date: 2024-12-24
     */
    @ApiOperation(value = "店铺申请表-编辑", notes = "店铺申请表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody StoreApply storeApply) {
        storeApplyService.updateById(storeApply);
        return Result.ok("编辑成功!");
    }

    @ApiOperation(value = "店铺申请审核")
    @PutMapping(value = "/audit")
    public Result<?> audit(@RequestBody StoreApply request) {
        StoreApply storeApply = storeApplyService.getById(request.getId());
        if (storeApply.getStatus() != _Whole.ZERO)
            return Result.error("已审核");
        LambdaUpdateChainWrapper<StoreApply> updateSql = storeApplyService.lambdaUpdate()
                .set(StoreApply::getStatus, request.getStatus())
                .eq(StoreApply::getId, request.getId());
        if (request.getStatus() == _Whole.ONE) {
            Store store = FastJsonHelper.beanToBean(storeApply, Store.class);
            store.setId(IDGeneratorUtils.getStringId());
            store.setApplyId(storeApply.getId());
            store.setCover(storeApply.getLogoImage());
            store.setMobile(storeApply.getStoreMobile());
            store.setSliderImage(storeApply.getInsideImages());
            store.setStatus(_Whole.ONE);
            storeService.save(store);
            updateSql.set(StoreApply::getStoreId, store.getId());
        }
        updateSql.update();
        return Result.ok("审核成功");
    }

    /**
     * @param id
     * @return
     * @description: 通过id删除
     * @author: FanJq
     * @date: 2024-12-24
     */
    @ApiOperation(value = "店铺申请表-通过id删除", notes = "店铺申请表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        storeApplyService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * @param ids
     * @return
     * @description: 批量删除
     * @author: FanJq
     * @date: 2024-12-24
     */
    @ApiOperation(value = "店铺申请表-批量删除", notes = "店铺申请表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.storeApplyService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * @param id
     * @return
     * @description: 通过id查询
     * @author: FanJq
     * @date: 2024-12-24
     */
    @ApiOperation(value = "店铺申请表-通过id查询", notes = "店铺申请表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        StoreApply storeApply = storeApplyService.getById(id);
        return Result.ok(storeApply);
    }

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(String title, String storeMobile, String legalPerson, String mobile, Integer status, String fileName) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // Step.2 获取导出数据
        List<StoreApplyAdmin> exportList = storeApplyService.getAdminList(title, storeMobile, legalPerson, mobile, status);
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, fileName); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, StoreApplyAdmin.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(fileName, "导出人:" + sysUser.getRealname(), fileName));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }

    /**
     * @param request
     * @param response
     * @return
     * @description: 通过excel导入数据
     * @author: FanJq
     * @date: 2024-12-24
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, StoreApply.class);
    }
}
